package ntd.util.modular.function.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Entity;
import com.c.opt.tool.ToolEntity;
import com.tool.ToolRedisson;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;
import ntd.util.modular.function.entity.ChannelBusiness;
import org.redisson.api.RBucket;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class UtilChannelBusiness {

    public static ChannelBusiness getById(String id) {
        RBucket<ChannelBusiness> bucket = ToolRedisson.me.redisson.getBucket("channel-business-" + id);
        if (!bucket.isExists()) {
            Entity entity = new ToolMysql(FactoryData.dataSource()).findById("function_channel_business", id);
            if (ObjectUtil.isNull(entity)) {
                throw new ComException("找不到渠道商信息");
            }
            bucket.set(ToolEntity.toBean(entity, ChannelBusiness.class), Duration.ofSeconds(60 * 5));
        }
        return bucket.get();
    }
}
